/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kamus.manager;

import com.kamus.DB;
import com.kamus.model.Kosakata;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author omega
 */
public class KosakataManager {

    public Connection connection;
    private static KosakataManager instance;

    public KosakataManager() {
        connection = getConnection();
    }

    public static KosakataManager getInstance() {
        if (instance == null) {
            instance = new KosakataManager();
        }
        return instance;
    }

    public Connection getConnection() {
        if (connection == null) {
            connection = DB.getConnection();
        }
        return connection;
    }

    public void cekConnection() {
        this.connection = getConnection();
    }

    public void insertKata(Kosakata kata) {
        cekConnection();
        try {
            PreparedStatement psmnt = connection.prepareStatement("insert into kosakata_sla_ind(slank,indonesia) values(?,?)");
            psmnt.setString(1, kata.getGaul());
            psmnt.setString(2, kata.getIndonesia());
            psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void deleteKata(int id) {
        cekConnection();
        try {
            PreparedStatement psmnt = connection.prepareStatement("delete from kosakata_sla_ind where ID_KOSAKATA=?");
            psmnt.setInt(1, id);
            psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ArrayList<Kosakata> getKatas() {
        cekConnection();
        ArrayList<Kosakata> list = new ArrayList<Kosakata>();
        Kosakata kata;
        try {
            PreparedStatement psmnt = connection.prepareStatement("select * from kosakata_sla_ind order by slank");
            ResultSet rs = psmnt.executeQuery();
            while (rs.next()) {
                kata = new Kosakata();
                kata.setId(rs.getInt("ID_KOSAKATA"));
                kata.setIndonesia(rs.getString("INDONESIA"));
                kata.setGaul(rs.getString("SLANK"));
                list.add(kata);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }

    public void updateKata(Kosakata kata) {
        cekConnection();
        try {
            PreparedStatement psmnt = connection.prepareStatement("update kosakata_sla_ind set indonesia=? slank=? where ID_KOSAKATA= ?");
            psmnt.setString(1, kata.getIndonesia());
            psmnt.setString(2, kata.getGaul());
            psmnt.setInt(1, kata.getId());
            psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
